//
//  CCSDChouseSortView.m
//  Ccsd_user
//
//  Created by 张露杰 on 18/2/24.
//  Copyright © 2018年 张露杰. All rights reserved.
//

#import "CCSDChouseKindView.h"

#import "CCSDChouseKindCell.h"

@interface CCSDChouseKindView()
@property (nonatomic, strong) UITableView *table;
@end

@implementation CCSDChouseKindView
{
    NSArray *_datas;
    NSInteger _selectIndex;
    NSDictionary *_selectData;
}

- (instancetype)initDatas:(NSArray *)datas selectData:(NSDictionary *)selectData
{
    self = [super init];
    if (self) {
        _datas = datas;
        _selectIndex = [datas indexOfObject:selectData];
        _selectData = selectData;
        [self creatView];
    }
    return self;
}

-(void)creatView
{
    self.frame = CGRectMake(0, 40*screenW_6, SCREENW, SCREENH - 64 - 40*screenW_6);
    
    /**
     底部遮罩
     */
    UIView *backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    backGroundView.backgroundColor = [UIColor blackColor];
    backGroundView.alpha = 0.5;
    [self addSubview:backGroundView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    [backGroundView addGestureRecognizer:tap];
    [self addSubview:self.table];
}

-(UITableView *)table
{
    if (!_table) {
        _table = [[UITableView alloc]initWithFrame:CGRectMake(0,0, self.frame.size.width, _datas.count*40*screenW_6>self.frame.size.height?self.frame.size.height:_datas.count*40*screenW_6) style:UITableViewStylePlain];
        _table.delegate = self;
        _table.dataSource = self;
        _table.showsHorizontalScrollIndicator = NO;
        _table.showsVerticalScrollIndicator = NO;
        _table.separatorStyle = 0;
        _table.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        _table.bounces = NO;
    }
    return _table;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return .1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return .1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CCSDChouseKindCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CCSDChouseKindCell"];
    if (!cell) {
        cell = [[CCSDChouseKindCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CCSDChouseKindCell"];
    }
    [cell loadCellNewWithData:_datas[indexPath.row] isSelect:_selectIndex==indexPath.row?YES:NO];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40*screenW_6;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _selectData = _datas[indexPath.row];
    if (_complete) {
        _complete(_selectData);
    }
    [self dismiss];
}

-(void)dismiss
{
    if (_dismissComplete) {
        _dismissComplete();
    }
    [self removeFromSuperview];
}

-(void)showOnView:(UIView *)view
{
    if (!view) {
        return;
    }
    [view addSubview:self];
}

@end
